
-- by Chris Mackey

--pieces
local body = piece "body"
local head = piece "head"
local club = piece "club"

local l_arm = piece "l_arm"
local l_hand = piece "l_hand"
local l_thigh = piece "l_thigh"
local l_foot = piece "l_foot"

local r_arm = piece "r_arm"
local r_hand = piece "r_hand"
local r_thigh = piece "r_thigh"
local r_foot = piece "r_foot"

--signals
local walk = 2
local aim  = 4

--constants
local lefty = 0.8
local righty = 20
local turny = 0.6

local function Walk()
	SetSignalMask( walk )
	while( true ) do 
		Turn( body, y_axis, -turny, lefty )
		Turn( head, y_axis, turny, lefty )
		Turn( l_arm, y_axis, turny, lefty )
		Turn( r_arm, y_axis, turny, lefty )
		
		Turn( l_thigh, y_axis, turny, lefty )
		
		Move( head, y_axis, -7, 20 )
		Turn( r_thigh, y_axis, turny, lefty )
		Move( r_foot, y_axis, 0, 80 )
		Move( r_foot, z_axis, -25, 20 )
		
		WaitForMove( r_foot, y_axis )
		Move( head, y_axis, 2, 5 )
		Move( l_foot, y_axis, 10, 15 )
		Move( l_foot, z_axis, righty, 18 )
		
		WaitForTurn( body, y_axis )
		--Sleep( 100 )
		
		Turn( body, y_axis, turny, lefty )
		Turn( head, y_axis, -turny, lefty )
		Turn( l_arm, y_axis, -turny, lefty )
		Turn( r_arm, y_axis, -turny, lefty )
		
		Move( head, y_axis, -7, 20 )
		Turn( l_thigh, y_axis, -turny, lefty )
		Move( l_foot, y_axis, 0, 80 )
		Move( l_foot, z_axis, -25, 20 )
		
		Turn( r_thigh, y_axis, -turny, lefty )
		
		WaitForMove( l_foot, y_axis )
		Move( head, y_axis, 2, 5 )
		Move( r_foot, y_axis, 10, 15 )
		Move( r_foot, z_axis, righty, 18 )
		
		WaitForTurn( body, y_axis )
		--Sleep( 100 )
	end
end

local function HoverPieces()
	local bodymove  = math.random(-2, -1)
	local legsmovel = math.random(-5, -2)
	local legsmover = math.random(-5, -2)
	
	local bodymove2  = math.random(1, 5)
	local legsmove2l = math.random(1, 3)
	local legsmove2r = math.random(1, 3)
	while( true ) do
		--arms and head
		Move( head, y_axis, math.random(-3,3), .8 )
		Move( l_arm, y_axis, math.random(1,5), 1 )
		Move( l_hand, y_axis, math.random(1,7), 1 )
		Sleep( math.random(100,500) )
		Move( r_arm, y_axis, math.random(1,5), 1 )
		Move( r_hand, y_axis, math.random(1,8), 1 )
		Sleep( math.random(500,800) )
		
		--body and legs
		Move( body, y_axis, bodymove, 1 )
		Move( l_thigh, y_axis, legsmovel, 2 )
		Move( r_thigh, y_axis, legsmover, 2 )
		Move( l_foot, y_axis, (-legsmovel-bodymove), 3 )
		Move( r_foot, y_axis, (-legsmover-bodymove), 3 )
		Sleep( math.random(500,800) )		
		
		--arms and head
		Move( l_arm, y_axis, math.random(-6,-1), 1 )
		Move( l_hand, y_axis, math.random(-6,-1), 1 )
		Sleep( math.random(100,500) )
		Move( r_arm, y_axis, math.random(-6,-1), 1 )
		Move( r_hand, y_axis, math.random(-6,-1), 1 )
		Sleep( math.random(500,800) )
		
		--body and legs
		Move( body, y_axis, bodymove2, 1 )
		Move( l_thigh, y_axis, legsmove2l, 2 )
		Move( r_thigh, y_axis, legsmove2r, 2 )
		Move( l_foot, y_axis, (-legsmove2l-bodymove2), 3 )
		Move( r_foot, y_axis, (-legsmove2r-bodymove2), 3 )
		Sleep( math.random(500,800) )
	end
end

local function StopWalk()
	Turn( body, y_axis, 0, 1 )
	Turn( head, y_axis, 0, 1 )
	
	Turn( l_thigh, y_axis, 0, 1 )
	Move( l_foot, y_axis, 0, 25 )
	Move( l_foot, z_axis, 0, 25 )
	
	Turn( r_thigh, y_axis, 0, 1 )
	Move( r_foot, y_axis, 0, 25 )
	Move( r_foot, z_axis, 0, 25 )
	
	
	Turn( r_arm, y_axis, 0, 1 )
	Turn( r_arm, x_axis, 0, 1 )
	Turn( r_arm, z_axis, 0, 1 )
	
	Turn( r_hand, y_axis, 0, 1 )
	Turn( r_hand, x_axis, 0, 1 )
	Turn( r_hand, z_axis, 0, 1 )
	
	Turn( l_arm, y_axis, 0, 1 )
	Turn( l_arm, x_axis, 0, 1 )
	Turn( l_arm, z_axis, 0, 1 )
	
	Turn( l_hand, y_axis, 0, 1 )
	Turn( l_hand, x_axis, 0, 1 )
	Turn( l_hand, z_axis, 0, 1 )
	
end

function script.Create()
	Hide( club )
	StartThread( HoverPieces )
end

function script.StartMoving()
	--Signal( walk )
	SetSignalMask( walk )
	StartThread( Walk )
end

function script.StopMoving()
	Signal( walk )
	SetSignalMask( walk )
	StartThread( StopWalk )
end

function script.QueryWeapon1() return l_hand end

function script.AimFromWeapon1() return head end

function script.AimWeapon1( heading, pitch )
	Signal( aim )
	SetSignalMask( aim )
	
	Turn( head, y_axis, heading, 3 )
	
	Turn( l_arm, y_axis, heading, 2 )
	Turn( l_arm, x_axis, -2.2, 1.5 )
	Turn( l_hand, x_axis, 0, 2 )
	Move( l_hand, y_axis, -3, 20 )
	Move( l_hand, z_axis, 0, 20 )
	
	WaitForTurn( l_arm, x_axis )
	WaitForTurn( l_arm, y_axis )
	
	return true
end

function script.FireWeapon1()
	Turn( l_arm, x_axis, -.2, 10 )
	Turn( l_hand, x_axis, -.2, 8 )
	Move( l_hand, y_axis, -50, 80 )
end

function script.Killed()
	Explode( head, SFX.EXPLODE )
	Explode( body, SFX.EXPLODE )
	Explode( club, SFX.EXPLODE )
	Explode( l_arm, SFX.EXPLODE )
	Explode( l_hand, SFX.EXPLODE )
	Explode( l_thigh, SFX.EXPLODE )
	Explode( l_foot, SFX.EXPLODE )
	Explode( r_arm, SFX.EXPLODE )
	Explode( r_hand, SFX.EXPLODE )
	Explode( r_thigh, SFX.EXPLODE )
	Explode( r_foot, SFX.EXPLODE )
end
